package com.qiu.usercenter.config;

import com.google.gson.Gson;
import lombok.Data;
import okhttp3.*;

import java.util.*;


@Data
public class AIConfig {

    private String apiKey;
    private String baseUrl;

    private String chatId;
    private boolean stream;
    private boolean detail;
    private String responseChatItemId;
    private Map<String, String> variables;
    private String content;

    public AIConfig setApiKey(String apiKey) {
        this.apiKey = apiKey;
        return this;
    }
    public AIConfig setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
        return this;
    }

    public AIConfig setChatId(String chatId) {
        this.chatId = chatId;
        return this;
    }

    public AIConfig setStream(boolean stream) {
        this.stream = stream;
        return this;
    }

    public AIConfig setDetail(boolean detail) {
        this.detail = detail;
        return this;
    }

    public AIConfig setResponseChatItemId(String responseChatItemId) {
        this.responseChatItemId = responseChatItemId;
        return this;
    }

    public AIConfig setVariables(Map<String, String> variables) {
        this.variables = variables;
        return this;
    }

    public AIConfig setContent(String content) {
        this.content = content;
        return this;
    }

    public Request build() {
        // 构建请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("chatId", chatId);
        requestBody.put("stream", stream);
        requestBody.put("detail", detail);
        requestBody.put("responseChatItemId", responseChatItemId);
        requestBody.put("variables", variables);

        List<Map<String, String>> messages = new ArrayList<>();
        Map<String, String> userMessage = new HashMap<>();
        userMessage.put("role", "user");
        userMessage.put("content", content);
        messages.add(userMessage);
        requestBody.put("messages", messages);

        // 将请求体转换为 JSON 字符串
        Gson gson = new Gson();
        String jsonBody = gson.toJson(requestBody);

        // AI 请求客户端
        RequestBody requestBodyEntity = RequestBody.create(MediaType.parse("application/json"), jsonBody);
        return new Request.Builder()
                .url(baseUrl)
                .addHeader("Authorization", "Bearer " + apiKey)
                .addHeader("Content-Type", "application/json")
                .post(requestBodyEntity)
                .build();
    }
}